//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MultiStepInverseFloater.h"
using namespace Cephei::QL::Models::Marketmodels::Products::Multistep;
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProductCashFlow.h>
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/Products/MultiProductMultiStep.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Models::Marketmodels::Products;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::CMultiStepInverseFloater (Cephei::IVector<Double>^ rateTimes, Cephei::IVector<Double>^ fixedAccruals, Cephei::IVector<Double>^ floatingAccruals, Cephei::IVector<Double>^ fixedStrikes, Cephei::IVector<Double>^ fixedMultipliers, Cephei::IVector<Double>^ floatingSpreads, Cephei::IVector<Double>^ paymentTimes, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payer) : CMultiProductMultiStep(CMultiStepInverseFloater::typeid)
{
    CoVector<Double>^ _CrateTimes;
    CoVector<Double>^ _CfixedAccruals;
    CoVector<Double>^ _CfloatingAccruals;
    CoVector<Double>^ _CfixedStrikes;
    CoVector<Double>^ _CfixedMultipliers;
    CoVector<Double>^ _CfloatingSpreads;
    CoVector<Double>^ _CpaymentTimes;
    try
    {
#ifdef HANDLE
        _phMultiStepInverseFloater = NULL;
#endif
        CoVector<Double>^ _CrateTimes = safe_cast<CoVector<Double>^> (rateTimes);
        _CrateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = _CrateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        CoVector<Double>^ _CfixedAccruals = safe_cast<CoVector<Double>^> (fixedAccruals);
        _CfixedAccruals->Lock();
        INativeVector<Double>^ _NCIfixedAccruals = _CfixedAccruals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfixedAccruals = safe_cast<CDoubleVector^>(_NCIfixedAccruals);
        std::vector<QuantLib::Real>& _fixedAccruals = static_cast<std::vector<QuantLib::Real>&> (_NCfixedAccruals->GetReference ());
        CoVector<Double>^ _CfloatingAccruals = safe_cast<CoVector<Double>^> (floatingAccruals);
        _CfloatingAccruals->Lock();
        INativeVector<Double>^ _NCIfloatingAccruals = _CfloatingAccruals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfloatingAccruals = safe_cast<CDoubleVector^>(_NCIfloatingAccruals);
        std::vector<QuantLib::Real>& _floatingAccruals = static_cast<std::vector<QuantLib::Real>&> (_NCfloatingAccruals->GetReference ());
        CoVector<Double>^ _CfixedStrikes = safe_cast<CoVector<Double>^> (fixedStrikes);
        _CfixedStrikes->Lock();
        INativeVector<Double>^ _NCIfixedStrikes = _CfixedStrikes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfixedStrikes = safe_cast<CDoubleVector^>(_NCIfixedStrikes);
        std::vector<QuantLib::Real>& _fixedStrikes = static_cast<std::vector<QuantLib::Real>&> (_NCfixedStrikes->GetReference ());
        CoVector<Double>^ _CfixedMultipliers = safe_cast<CoVector<Double>^> (fixedMultipliers);
        _CfixedMultipliers->Lock();
        INativeVector<Double>^ _NCIfixedMultipliers = _CfixedMultipliers->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfixedMultipliers = safe_cast<CDoubleVector^>(_NCIfixedMultipliers);
        std::vector<QuantLib::Real>& _fixedMultipliers = static_cast<std::vector<QuantLib::Real>&> (_NCfixedMultipliers->GetReference ());
        CoVector<Double>^ _CfloatingSpreads = safe_cast<CoVector<Double>^> (floatingSpreads);
        _CfloatingSpreads->Lock();
        INativeVector<Double>^ _NCIfloatingSpreads = _CfloatingSpreads->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfloatingSpreads = safe_cast<CDoubleVector^>(_NCIfloatingSpreads);
        std::vector<QuantLib::Real>& _floatingSpreads = static_cast<std::vector<QuantLib::Real>&> (_NCfloatingSpreads->GetReference ());
        CoVector<Double>^ _CpaymentTimes = safe_cast<CoVector<Double>^> (paymentTimes);
        _CpaymentTimes->Lock();
        INativeVector<Double>^ _NCIpaymentTimes = _CpaymentTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCpaymentTimes = safe_cast<CDoubleVector^>(_NCIpaymentTimes);
        std::vector<QuantLib::Time>& _paymentTimes = static_cast<std::vector<QuantLib::Time>&> (_NCpaymentTimes->GetReference ());
        bool _payer = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (payer) ? (bool)ValueHelper::Convert (payer->Value) : true); //4
        _ppMultiStepInverseFloater = new boost::shared_ptr<QuantLib::MultiStepInverseFloater> (new QuantLib::MultiStepInverseFloater ( _rateTimes,  _fixedAccruals,  _floatingAccruals,  _fixedStrikes,  _fixedMultipliers,  _floatingSpreads,  _paymentTimes,  _payer ));
        SetMultiProductMultiStep (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepInverseFloater));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CrateTimes != nullptr) _CrateTimes->Unlock();
        if (_CfixedAccruals != nullptr) _CfixedAccruals->Unlock();
        if (_CfloatingAccruals != nullptr) _CfloatingAccruals->Unlock();
        if (_CfixedStrikes != nullptr) _CfixedStrikes->Unlock();
        if (_CfixedMultipliers != nullptr) _CfixedMultipliers->Unlock();
        if (_CfloatingSpreads != nullptr) _CfloatingSpreads->Unlock();
        if (_CpaymentTimes != nullptr) _CpaymentTimes->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::CMultiStepInverseFloater (boost::shared_ptr<QuantLib::MultiStepInverseFloater>& childNative, Object^ owner) : CMultiProductMultiStep(CMultiStepInverseFloater::typeid)
{
#ifdef HANDLE
	_phMultiStepInverseFloater = NULL;
#endif
	_ppMultiStepInverseFloater = &childNative;
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepInverseFloater));
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::CMultiStepInverseFloater (QuantLib::MultiStepInverseFloater& childNative, Object^ owner) : CMultiProductMultiStep(CMultiStepInverseFloater::typeid)
{
#ifdef HANDLE
	_phMultiStepInverseFloater = NULL;
#endif
	_ppMultiStepInverseFloater = new boost::shared_ptr<QuantLib::MultiStepInverseFloater> (&childNative);
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepInverseFloater));
    _MultiStepInverseFloaterOwner = owner;
    _MultiProductMultiStepOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::CMultiStepInverseFloater (CMultiStepInverseFloater^ copy) : CMultiProductMultiStep(CMultiStepInverseFloater::typeid)
{
#ifdef HANDLE
	_phMultiStepInverseFloater = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMultiStepInverseFloater = new boost::shared_ptr<QuantLib::MultiStepInverseFloater> (copy->GetShared());
        _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepInverseFloater));
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::CMultiStepInverseFloater (System::Type^ t) : CMultiProductMultiStep(CMultiStepInverseFloater::typeid)
{
#ifdef HANDLE
	_phMultiStepInverseFloater = NULL;
#endif
	if (!t->IsSubclassOf(CMultiStepInverseFloater::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::CMultiStepInverseFloater (QuantLib::Handle<QuantLib::MultiStepInverseFloater>& childNative, Object^ owner)  : CMultiProductMultiStep(CMultiStepInverseFloater::typeid)
{
	_phMultiStepInverseFloater = &childNative;
	_ppMultiStepInverseFloater = &static_cast<boost::shared_ptr<QuantLib::MultiStepInverseFloater>>(childNative.currentLink());
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepInverseFloater));
    _MultiStepInverseFloaterOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::CMultiStepInverseFloater (QuantLib::Handle<QuantLib::MultiStepInverseFloater> childNative)  : CMultiProductMultiStep(CMultiStepInverseFloater::typeid)
{
	_phMultiStepInverseFloater = &childNative;
	_ppMultiStepInverseFloater = &static_cast<boost::shared_ptr<QuantLib::MultiStepInverseFloater>>(childNative.currentLink());
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepInverseFloater));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::CMultiStepInverseFloater (QuantLib::MultiStepInverseFloater childNative)  : CMultiProductMultiStep(CMultiStepInverseFloater::typeid)
{
#ifdef HANDLE
	_phMultiStepInverseFloater = NULL;
#endif
	_ppMultiStepInverseFloater = new boost::shared_ptr<QuantLib::MultiStepInverseFloater> (new QuantLib::MultiStepInverseFloater (childNative));
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppMultiStepInverseFloater));
}
#endif

Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::~CMultiStepInverseFloater ()
{
    if (_ppMultiStepInverseFloater != NULL)
    {
	    delete _ppMultiStepInverseFloater;
        _ppMultiStepInverseFloater = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::!CMultiStepInverseFloater ()
{
    if (_ppMultiStepInverseFloater != NULL)
    {
	    delete _ppMultiStepInverseFloater;
    }
}
QuantLib::MultiStepInverseFloater& Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::GetReference ()
{
    if (_ppMultiStepInverseFloater == NULL) throw gcnew NativeNullException ();
	return **_ppMultiStepInverseFloater;
}
boost::shared_ptr<QuantLib::MultiStepInverseFloater>& Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::GetShared ()
{
    if (_ppMultiStepInverseFloater == NULL) throw gcnew NativeNullException ();
	return *_ppMultiStepInverseFloater;
}
QuantLib::MultiStepInverseFloater* Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::GetPointer ()
{
    if (_ppMultiStepInverseFloater == NULL) throw gcnew NativeNullException ();
	return &**_ppMultiStepInverseFloater;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MultiStepInverseFloater>& Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::GetHandle ()
{
	if (_phMultiStepInverseFloater == NULL)
	{
		_phMultiStepInverseFloater = new Handle<QuantLib::MultiStepInverseFloater> (*_ppMultiStepInverseFloater);
	}
	return *_phMultiStepInverseFloater;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::HasNative () 
{
	return (_ppMultiStepInverseFloater != NULL);
}

UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMultiStepInverseFloater)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMultiStepInverseFloater)->numberOfProducts ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppMultiStepInverseFloater)->possibleCashFlowTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::IMultiStepInverseFloater^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMultiStepInverseFloater)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater::NextTimeStep (Cephei::QL::Models::Marketmodels::ICurveState^ currentState, Cephei::IVector<UInt64>^ numberCashFlowsThisStep, Cephei::IMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ cashFlowsGenerated)
{
    CCurveState^ _CcurrentState;
    CoVector<UInt64>^ _CnumberCashFlowsThisStep;
    CoMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ _CcashFlowsGenerated;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
        _CnumberCashFlowsThisStep = safe_cast<CoVector<UInt64>^> (numberCashFlowsThisStep);
        _CnumberCashFlowsThisStep->Lock();
        INativeVector<UInt64>^ _NCInumberCashFlowsThisStep = _CnumberCashFlowsThisStep->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCnumberCashFlowsThisStep = safe_cast<CUInt64Vector^>(_NCInumberCashFlowsThisStep);
        std::vector<QuantLib::Size>& _numberCashFlowsThisStep = static_cast<std::vector<QuantLib::Size>&> (_NCnumberCashFlowsThisStep->GetReference ());
        _CcashFlowsGenerated = safe_cast<CoMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^> (cashFlowsGenerated);
        _CcashFlowsGenerated->Lock();
        INativeMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ _NCIcashFlowsGenerated = _CcashFlowsGenerated->getFeature (NativeFeature::Value);
        CMarketModelMultiProductCashFlowMatrix^ _NCcashFlowsGenerated = safe_cast<CMarketModelMultiProductCashFlowMatrix^>(_NCIcashFlowsGenerated);
        std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >& _cashFlowsGenerated = static_cast<std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >&> (_NCcashFlowsGenerated->GetReference ());
    	bool _rv = (bool)(*_ppMultiStepInverseFloater)->nextTimeStep ( _currentState,  _numberCashFlowsThisStep,  _cashFlowsGenerated );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
        if (_CnumberCashFlowsThisStep != nullptr) _CnumberCashFlowsThisStep->Unlock();
        if (_CcashFlowsGenerated != nullptr) _CcashFlowsGenerated->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Products::Multistep::IMultiStepInverseFloater^ Cephei::QL::Models::Marketmodels::Products::Multistep::CMultiStepInverseFloater_Factory::Create (Cephei::IVector<Double>^ rateTimes, Cephei::IVector<Double>^ fixedAccruals, Cephei::IVector<Double>^ floatingAccruals, Cephei::IVector<Double>^ fixedStrikes, Cephei::IVector<Double>^ fixedMultipliers, Cephei::IVector<Double>^ floatingSpreads, Cephei::IVector<Double>^ paymentTimes, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payer)
{
    return gcnew CMultiStepInverseFloater ( rateTimes,  fixedAccruals,  floatingAccruals,  fixedStrikes,  fixedMultipliers,  floatingSpreads,  paymentTimes,  payer);
}
